package com.example.rtsp2.config;
import com.example.rtsp2.handler.VideoStreamHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.HandshakeInterceptor;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;

import java.util.Map;

/**
 * @Author: 杜科璇
 * @Description: WebSocket配置
 * @Date: 2025/9/5 17:55
 * @Version: 1.0
 */


@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    private final VideoStreamHandler videoStreamHandler;

    public WebSocketConfig(VideoStreamHandler videoStreamHandler) {
        this.videoStreamHandler = videoStreamHandler;
    }

    /**
     * 注册WebSocket处理器
     * @param registry
     */
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(videoStreamHandler, "/video-stream")
                .setAllowedOrigins("*")
                .addInterceptors(new HandshakeInterceptor() {

                    /**
                     * 在握手之前执行该方法，如果返回false，则不会继续执行后面的拦截器
                     * @param request
                     * @param response
                     * @param wsHandler
                     * @param attributes
                     * @return
                     */
                    @Override
                    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response,
                                                   WebSocketHandler wsHandler, Map<String, Object> attributes) {
                        return true;
                    }

                    /**
                     * 在握手之后执行该方法
                     * @param request
                     * @param response
                     * @param wsHandler
                     * @param exception
                     */

                    @Override
                    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response,
                                               WebSocketHandler wsHandler, Exception exception) {
                    }
                });
    }

    /**
     * 创建WebSocket容器工厂
     * @return
     */
    @Bean
    public ServletServerContainerFactoryBean createWebSocketContainer() {
        ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
        // 增加消息缓冲区大小
        container.setMaxTextMessageBufferSize(512 * 1024); // 512KB
        container.setMaxBinaryMessageBufferSize(512 * 1024); // 512KB
        // 增加会话空闲超时时间
        container.setMaxSessionIdleTimeout(300000L); // 5分钟
        // 增加异步发送超时时间
        container.setAsyncSendTimeout(60000L); // 60秒
        return container;
    }
}

